Imports System.Web
Imports System.Text.RegularExpressions
Public Class IWeb_Form
    Dim IWeb As New IWeb.WebService

#Region "Properties"
    Public ReadOnly Property WebserviceURL() As String
        Get
            Return Me.txtWebsite.Text & "/DesktopModules/IWeb/webservice.asmx"
        End Get
    End Property
    Public ReadOnly Property Password() As String
        Get
            Dim strPassword As String = ""
            If Me.chkEncrypted.Checked Then
                Dim objIWebEncryption As New IWebEncryption
                strPassword = objIWebEncryption.Encryption(Me.txtPassword.Text, Me.txtKey.Text)
                strPassword = HttpUtility.UrlEncode(strPassword)
            Else
                strPassword = Me.txtPassword.Text
            End If
            Return strPassword
        End Get
    End Property
#End Region

    Private Sub btnGetUsername_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetUsername.Click
        IWeb.Url = WebserviceURL
        Dim strUsername As String = ""
        strUsername = IWeb.GetUserName(Me.txtPortal.Text, Me.txtUsername.Text, Password, Me.chkEncrypted.Checked)
        Me.lblGetUserName.Text = strUsername
    End Sub

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
        Dim strResponse As String = ""
        IWeb.Url = WebserviceURL
        Try
            strResponse = IWeb.EncryptData(TextBox1.Text, Me.txtKey.Text)
            Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)
        Catch ex As Exception
            TextBox2.Text = ex.Message
        End Try
    End Sub

    Private Sub btnUnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnEncrypt.Click
        Dim strResponse As String = ""
        IWeb.Url = WebserviceURL
        Try
            strResponse = IWeb.UnEncryptData(HttpUtility.UrlEncode(TextBox1.Text), Me.txtKey.Text)
            Me.TextBox2.Text = HttpUtility.UrlDecode(strResponse)
        Catch ex As Exception
            TextBox2.Text = ex.Message
        End Try
    End Sub

    Private Sub btnGetVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetVersion.Click
        Dim IWebVersion_Info As IWebVersion_Info
        Try
            IWebVersion_Info = IWebVersion.GetVersion()
            Me.lblGetVersion.Text = "Version: " & IWebVersion_Info.VersionName & " (" & IWebVersion_Info.VersionNumber.ToString() & ")"
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnPortals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPortals.Click
        Dim i As Integer = 0
        Dim arrPortals As Array
        Dim colPortals As String = ""
        Dim strPortalNumber As String
        Dim strPortalName As String
        Dim strPortalEntry As String
        Try
            IWeb.Url = WebserviceURL
            arrPortals = IWeb.PortalsOnServer().Split(",")
            For i = 0 To arrPortals.Length - 1 Step 2
                strPortalNumber = arrPortals(i).ToString()
                strPortalName = arrPortals(i + 1).ToString()
                strPortalEntry = strPortalNumber & " [" & strPortalName & "]"
                colPortals = colPortals & strPortalEntry & vbCrLf
            Next
            lblPortals.Text = colPortals.ToString
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub btnCheckVersion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckVersion.Click
        If IWebVersion.CheckVersion(10100) Then
            MessageBox.Show("The IWeb version on the server is correct")
        Else
            MessageBox.Show("The IWeb version on the server is too low")
        End If
    End Sub

    Private Sub btnGetUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetUsers.Click
        Dim colUserInfo() As IWeb.UserInfo
        IWeb.Url = WebserviceURL
        Try
            colUserInfo = IWeb.GetAllUsers(Me.txtPortal.Text, Me.txtUsername.Text, Password, Me.chkEncrypted.Checked)
            DataGridView1.DataSource = colUserInfo
        Catch ex As Exception
            Me.lblGetVersion.Text = ex.Message
        End Try
    End Sub

    Private Sub DataGridViewCellEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        If e.ColumnIndex > 0 Then
            Try
                IWeb.Url = WebserviceURL
                Dim DGV As DataGridView = Me.DataGridView1
                Dim DGVRowColection As DataGridViewRowCollection
                Dim DGVrow As DataGridViewRow
                Dim DGVCellCollection As DataGridViewCellCollection

                DGVRowColection = DGV.Rows
                DGVrow = DGVRowColection.Item(e.RowIndex)
                DGVCellCollection = DGVrow.Cells

                Dim colUserInfo As New IWeb.UserInfo()
                colUserInfo.UserID = CInt(DGVCellCollection.Item(0).Value.ToString)
                colUserInfo.Username = DGVCellCollection.Item(1).Value.ToString
                colUserInfo.DisplayName = DGVCellCollection.Item(2).Value.ToString
                colUserInfo.FirstName = DGVCellCollection.Item(3).Value.ToString
                colUserInfo.LastName = DGVCellCollection.Item(4).Value.ToString
                colUserInfo.Email = DGVCellCollection.Item(5).Value.ToString

                IWeb.UpdateAUser(Me.txtPortal.Text, Me.txtUsername.Text, Password, Me.chkEncrypted.Checked, colUserInfo)
            Catch ex As Exception
                TextBox2.Text = ex.Message
            End Try

        Else
            DataGridView1.Refresh()
        End If
    End Sub

End Class
